<!doctype html><html lang=en dir=auto>
<head><meta charset=utf-8>
<meta http-equiv=x-ua-compatible content="IE=edge">
<meta name=viewport content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name=robots content="index, follow">
<title>Swift | wenbin</title>
<meta name=keywords content>
<meta name=description content>
<meta name=author content>
<link rel=canonical href=https://imwenbin.com/tags/swift/>
<link crossorigin=anonymous href=/assets/css/stylesheet.min.css rel="preload stylesheet" as=style>
<link rel=icon href=https://imwenbin.com/favicon.ico>
<link rel=icon type=image/png sizes=16x16 href=https://imwenbin.com/favicon16x16.png>
<link rel=icon type=image/png sizes=32x32 href=https://imwenbin.com/favicon32x32.png>
<link rel=apple-touch-icon href=https://imwenbin.com/apple_touch_icon.png>
<link rel=mask-icon href=https://imwenbin.com/safari_pinned_tab.svg>
<meta name=theme-color content="#2e2e33">
<meta name=msapplication-TileColor content="#2e2e33">
<meta name=generator content="Hugo 0.89.3">
<link rel=alternate type=application/rss+xml href=https://imwenbin.com/tags/swift/index.xml>
<noscript>
<style>#theme-toggle,.top-link{display:none}</style>
<style>@media(prefers-color-scheme:dark){:root{--theme:rgb(29, 30, 32);--entry:rgb(46, 46, 51);--primary:rgb(218, 218, 219);--secondary:rgb(155, 156, 157);--tertiary:rgb(65, 66, 68);--content:rgb(196, 196, 197);--hljs-bg:rgb(46, 46, 51);--code-bg:rgb(55, 56, 62);--border:rgb(51, 51, 51)}.list{background:var(--theme)}.list:not(.dark)::-webkit-scrollbar-track{background:0 0}.list:not(.dark)::-webkit-scrollbar-thumb{border-color:var(--theme)}}</style>
</noscript><meta property="og:title" content="Swift">
<meta property="og:description" content>
<meta property="og:type" content="website">
<meta property="og:url" content="https://imwenbin.com/tags/swift/"><meta property="og:site_name" content="wenbin">
<meta name=twitter:card content="summary">
<meta name=twitter:title content="Swift">
<meta name=twitter:description content>
</head>
<body class=list id=top>
<script>localStorage.getItem("pref-theme")==="dark"?document.body.classList.add('dark'):localStorage.getItem("pref-theme")==="light"?document.body.classList.remove('dark'):window.matchMedia('(prefers-color-scheme: dark)').matches&&document.body.classList.add('dark')</script>
<header class=header>
<nav class=nav>
<div class=logo>
<a href=https://imwenbin.com/ accesskey=h title="wenbin (Alt + H)">wenbin</a>
<span class=logo-switches>
<button id=theme-toggle accesskey=t title="(Alt + T)"><svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"/></svg><svg id="sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg>
</button>
</span>
</div>
<ul id=menu>
<li>
<a href=https://imwenbin.com/blog/ title=Blog>
<span>Blog</span>
</a>
</li>
<li>
<a href=https://imwenbin.com/archives title=Archive>
<span>Archive</span>
</a>
</li>
<li>
<a href=https://imwenbin.com/tags title=Tags>
<span>Tags</span>
</a>
</li>
</ul>
</nav>
</header>
<main class=main>
<header class=page-header><div class=breadcrumbs><a href=https://imwenbin.com/>Home</a>&nbsp;»&nbsp;<a href=https://imwenbin.com/tags/>Tags</a></div>
<h1>Swift</h1>
</header>
<article class="post-entry tag-entry">
<header class=entry-header>
<h2>「iOS」UITableView.Style.insetGrouped样式 及 代码实现圆角cell 方案
</h2>
</header>
<section class=entry-content>
<p>网上很少搜到 insetGrouped 的样式和用法，结合自己的项目，大概阐述一下用法，最下方包含自己实现的源码
升级iOS版本后，细心的小伙伴发现了自带的 备忘录 app的列表样式发生了更改，圆角cell算是现在比较新潮的设计，本文主要讲一下系统的实现方案和自己用代码的实现方案。
一、系统实现方案 1. insetGrouped用法 很简单，用 xib 或代码方式初始化tableView时选择Style为 insetGrouped ，样式如下：
⚠️注意点：
必须选择 insetGrouped 样式 上述样式需要多个group，如果是单个group样式或plain样式则不能实现此效果（如下图） api是iOS 13及以上才能使用 2. 单个group样式预览 了解了👆注意点后，如果有特殊情况，比如兼容iOS13及以下或单个group样式怎么处理呢？下面就给出实现方案
二、自定义代码实现方案 分为三步
1. 核心代码（可以放在BaseController里） /// 设置cell圆角 /// - Parameters: /// - cell: cell /// - indexPath: indexPath /// - tableView: tableView /// - needSetAlone: 是否需要单独设置每个cell都是圆角. 默认按section为单位整体设置, 如果一个section只有一个cell则全部设置圆角 public func setCornerRadiusForSectionCell(cell: UITableViewCell, indexPath: IndexPath, tableView: UITableView, needSetAlone: Bool, cellY: CGFloat) { //圆角半径 let cornerRadius:CGFloat = 10....</p>
</section>
<footer class=entry-footer><span title="2021-11-18 10:09:36 +0800 CST">⌚️ 2021-11-18</span></footer>
<a class=entry-link aria-label="post link to 「iOS」UITableView.Style.insetGrouped样式 及 代码实现圆角cell 方案" href=https://imwenbin.com/blog/iosuitableview.style.insetgrouped%E6%A0%B7%E5%BC%8F-%E5%8F%8A-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0%E5%9C%86%E8%A7%92cell-%E6%96%B9%E6%A1%88/></a>
</article>
<article class="post-entry tag-entry">
<header class=entry-header>
<h2>「iOS & Android」WebView获取图片、点击图片
</h2>
</header>
<section class=entry-content>
<p>获取思路：
注入JS代码获取 ‘img’ 图片，拼接成字符串返回，然后分割字符串得到每个图片的url 注入JS代码获取图片的点击事件 iOS iOS使用WKWebView
// MARK:- WKNavigationDelegate func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { // 注入JS代码获取图片url和个数 self.webView.evaluateJavaScript(""" function getImages(){\ var imgs = document.getElementsByTagName('img');\ var imgScr = '';\ for(var i=0;i&lt;imgs.length;i++){\ if (i == 0){ \ imgScr = imgs[i].src; \ } else {\ imgScr = imgScr +'***'+ imgs[i].src;\ } \ };\ return imgScr;\ }; """, completionHandler: nil) self.webView.evaluateJavaScript("getImages()") { [weak self] (result, error) in guard error == nil else { return } guard let resultString: String = result as?...</p>
</section>
<footer class=entry-footer><span title="2021-11-18 10:04:36 +0800 CST">⌚️ 2021-11-18</span></footer>
<a class=entry-link aria-label="post link to 「iOS & Android」WebView获取图片、点击图片" href=https://imwenbin.com/blog/ios-androidwebview%E8%8E%B7%E5%8F%96%E5%9B%BE%E7%89%87%E7%82%B9%E5%87%BB%E5%9B%BE%E7%89%87/></a>
</article>
<article class="post-entry tag-entry">
<header class=entry-header>
<h2>【Swift】WKWebView获取选中文本
</h2>
</header>
<section class=entry-content>
<p>使用WKWebView时选中文本弹出UIMenuController，在开发中会自定义menu，但是自定义的menu无法通过系统api获取到选中文本，并且WKWebView抛弃了UIWebView的 stringByEvaluatingJavaScriptFromString 方法，只用一行代码就能实现。
解决方案 在自定义menu的action实现里进行操作
self.webView.evaluateJavaScript("window.getSelection().toString()") { (response, error) in guard response != nil && error == nil else { return } self.selectText = response as? String print(self.selectText) } </p>
</section>
<footer class=entry-footer><span title="2021-11-18 09:50:36 +0800 CST">⌚️ 2021-11-18</span></footer>
<a class=entry-link aria-label="post link to 【Swift】WKWebView获取选中文本" href=https://imwenbin.com/blog/swiftwkwebview%E8%8E%B7%E5%8F%96%E9%80%89%E4%B8%AD%E6%96%87%E6%9C%AC/></a>
</article>
<article class="post-entry tag-entry">
<header class=entry-header>
<h2>Swift5 访问限制关键字
</h2>
</header>
<section class=entry-content>
<p>访问控制关键字 限制从高到低排列
private fileprivate internal public open 简单总结： private 只能在当前文件的当前模块的作用域使用 fileprivate 只能在当前文件内使用 internal 是默认访问级别，所有没有单独列出的都是这个权限 public 可以在外部文件访问，如果只需要访问就用它 open 可以在外部文件访问，如果需要继承和重写就用它
更多详细区别： https://swiftgg.gitbook.io/swift/swift-jiao-cheng/25_access_control</p>
</section>
<footer class=entry-footer><span title="2021-11-18 09:43:36 +0800 CST">⌚️ 2021-11-18</span></footer>
<a class=entry-link aria-label="post link to Swift5 访问限制关键字" href=https://imwenbin.com/blog/swift5-%E8%AE%BF%E9%97%AE%E9%99%90%E5%88%B6%E5%85%B3%E9%94%AE%E5%AD%97/></a>
</article>
<footer class=page-footer>
<nav class=pagination>
<a class=prev href=https://imwenbin.com/tags/swift/>« Prev Page</a>
</nav>
</footer>
</main>
<footer class=footer>
<span>&copy; 2021 <a href=https://imwenbin.com/>wenbin</a>.</span>
<span>
保留部分权利。
</span>
</footer>
<a href=#top aria-label="go to top" title="Go to Top (Alt + G)" class=top-link id=top-link accesskey=g><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 6" fill="currentcolor"><path d="M12 6H0l6-6z"/></svg>
</a>
<script>let menu=document.getElementById('menu');menu&&(menu.scrollLeft=localStorage.getItem("menu-scroll-position"),menu.onscroll=function(){localStorage.setItem("menu-scroll-position",menu.scrollLeft)}),document.querySelectorAll('a[href^="#"]').forEach(a=>{a.addEventListener("click",function(b){b.preventDefault();var a=this.getAttribute("href").substr(1);window.matchMedia('(prefers-reduced-motion: reduce)').matches?document.querySelector(`[id='${decodeURIComponent(a)}']`).scrollIntoView():document.querySelector(`[id='${decodeURIComponent(a)}']`).scrollIntoView({behavior:"smooth"}),a==="top"?history.replaceState(null,null," "):history.pushState(null,null,`#${a}`)})})</script>
<script>var mybutton=document.getElementById("top-link");window.onscroll=function(){document.body.scrollTop>800||document.documentElement.scrollTop>800?(mybutton.style.visibility="visible",mybutton.style.opacity="1"):(mybutton.style.visibility="hidden",mybutton.style.opacity="0")}</script>
<script>document.getElementById("theme-toggle").addEventListener("click",()=>{document.body.className.includes("dark")?(document.body.classList.remove('dark'),localStorage.setItem("pref-theme",'light')):(document.body.classList.add('dark'),localStorage.setItem("pref-theme",'dark'))})</script>
</body>
</html>